<template>
  <div class="login-body">
    <div class="login-container">
      <div class="head">
        <div class="name">
          <div class="title">管理员管理</div>
        </div>
      </div>
      <el-form label-position="top" :rules="state.rules" :model="state.ruleForm" ref="loginForm" class="login-form">
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model.trim="state.ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model.trim="state.ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 错误提示 -->
        <el-form-item v-if="errorMessage" class="error-message">
          <span style="color: #F56C6C">{{ errorMessage }}</span>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="submitForm">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import axios from '@/utils/axios'
import {reactive, ref} from 'vue'

const loginForm = ref(null)
import {useRouter, useRoute} from 'vue-router';

const router = useRouter();
const route = useRoute();
const errorMessage = ref('') // 定义错误信息变量

const state = reactive({
  ruleForm: {
    username: '',
    password: ''
  },
  checked: true,
  rules: {
    username: [
      {required: true, message: '账户不能为空', trigger: 'blur'} // 修正true的引号
    ],
    password: [
      {required: true, message: '密码不能为空', trigger: 'blur'} // 修正true的引号
    ]
  }
})
const submitForm = () => {
  // 先进行表单验证
  loginForm.value.validate((valid) => {
    if (valid) {
      errorMessage.value = '' // 清空之前的错误信息
      axios.post("/api/admins/login", state.ruleForm)
          .then(res => {
            // 假设后端返回格式包含code字段
            console.log("返回数据:", res)
            if (res.code == 200) {
              console.log("返回数据:", res)
              localStorage.setItem("adminId",res.data.adminId)
              router.push("/dashboard")
            } else {
              // 使用后端返回的错误信息或默认信息
              errorMessage.value = res.data?.message || '用户名或密码不正确，请重试。';
              console.error('登录失败：', errorMessage.value);
            }
          })
          .catch(err => {
            // 捕获网络错误
            errorMessage.value = '网络异常，请稍后再试';
            console.error('登录请求失败：', err);
          })
    }
  })
}

</script>

<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100vh; /* 确保全屏显示 */
  background-color: #f5f7fa; /* 轻微背景色区分 */
}

.login-container {
  width: 420px;
  height: 500px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}

.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}

.head img {
  width: 100px;
  height: 100px;
  margin-right: 20px;
}

.head .title {
  font-size: 28px;
  color: #1BAEAE;
  font-weight: bold;
}

.head .tips {
  font-size: 12px;
  color: #999;
}

.login-form {
  width: 70%;
  margin: 0 auto;
}

/* 修正样式穿透写法 */
.login-form ::v-deep .el-form--label-top .el-form-item__label {
  padding: 0;
}

.login-form ::v-deep .el-form-item {
  margin-bottom: 20px; /* 增加间距 */
}

.error-message {
  margin-bottom: 10px;
}
</style>
